#include "pos_timer.h"
#include "systick.h"
#include "gd32e23x.h"
#include "pos_param.h"
#include "gd32e230c_bsp.h"
#include "pos.h"

#define WAKEUP_KEY_PIN                   GPIO_PIN_0
#define WAKEUP_KEY_GPIO_PORT             GPIOA

volatile unsigned int Time13_Count;      
volatile unsigned int Time13_Count2;      
volatile unsigned int Time14_Count;      
volatile unsigned int Time14_Count2;      
volatile unsigned int Time15_Count;      
volatile unsigned int G_PulseCount;      
volatile unsigned int G_PulseCount2;     
volatile unsigned int PulseCounted;      
volatile unsigned int PulseTimeOut;      

volatile unsigned char G_Standby;

 
void init_timer13(void)
{
    /* -----------------------------------------------------------------------
    TIMER13CLK is 1MHz = 72MHz/(prescaler+1)=72M/72=1M
    TIMER13_T is 1us
    ----------------------------------------------------------------------- */
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER13);

    timer_deinit(TIMER13);

     
    timer_initpara.prescaler         = 720-1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 124;   
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER13, &timer_initpara);

     
    timer_auto_reload_shadow_enable(TIMER13);
    timer_update_event_enable(TIMER13);
    timer_update_source_config(TIMER13, TIMER_UPDATE_SRC_GLOBAL);
    timer_interrupt_flag_clear(TIMER13, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER13, TIMER_INT_UP);
    nvic_irq_enable(TIMER13_IRQn, 1U);
    timer_disable(TIMER13);
}

 
void init_timer14(void)
{
    /* TIMER2 configuration: input capture mode -------------------
    the external signal is connected to TIMER14 CH0 pin(PA1)
    the rising edge is used as active edge
    the TIMER14 CH0CV is used to compute the frequency value
    ------------------------------------------------------------ */
    timer_parameter_struct 		timer_initpara;

     
    rcu_periph_clock_enable(RCU_TIMER14);
     
    timer_deinit(TIMER14);
     
    timer_struct_para_init(&timer_initpara);
     
    timer_initpara.prescaler         = 7200 - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 10000 - 1;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_init(TIMER14, &timer_initpara);

     
    timer_auto_reload_shadow_enable(TIMER14);
    timer_update_event_enable(TIMER14);
    timer_update_source_config(TIMER14, TIMER_UPDATE_SRC_GLOBAL);
    timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER14, TIMER_INT_UP);
    nvic_irq_enable(TIMER14_IRQn, 1U);
    timer_disable(TIMER14);
}

 
void init_timer15(void)
{
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER15);

    timer_deinit(TIMER15);
     
    timer_initpara.prescaler         = 719;     
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 4999;    
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER15, &timer_initpara);

     
    timer_auto_reload_shadow_enable(TIMER15);
    timer_update_event_enable(TIMER15);
    timer_update_source_config(TIMER15, TIMER_UPDATE_SRC_GLOBAL);
    timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER15, TIMER_INT_UP);
    nvic_irq_enable(TIMER15_IRQn, 2U);
    timer_enable(TIMER15);
}

void init_exti_Hall(void)
{
#ifdef SEGLCD_V2206    
     //PA1 流量计
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_CFGCMP);

	gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
	gpio_output_options_set(GPIOA, GPIO_PUPD_NONE, GPIO_OSPEED_50MHZ, GPIO_PIN_1);

	 
	syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN1);
	 
	exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_BOTH); 
	exti_interrupt_flag_clear(EXTI_1);
	 
	nvic_irq_disable(EXTI0_1_IRQn);
#else
     
     
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_CFGCMP);

	gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_11);
	gpio_output_options_set(GPIOA, GPIO_PUPD_NONE, GPIO_OSPEED_50MHZ, GPIO_PIN_11);

	 
	syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN11);
	 
	exti_init(EXTI_11, EXTI_INTERRUPT, EXTI_TRIG_BOTH); 
	exti_interrupt_flag_clear(EXTI_11);
	 
	nvic_irq_disable(EXTI4_15_IRQn);
#endif
}

#ifdef DUAL_MODE
volatile unsigned int G_PulseCount12;   
volatile unsigned int G_PulseCount12_1;     
volatile unsigned int PulseTimeOut12;   

void init_exti12(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_CFGCMP);

    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_12);
    gpio_output_options_set(GPIOA, GPIO_PUPD_NONE, GPIO_OSPEED_50MHZ, GPIO_PIN_12);

     
    syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN12);
     
    exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_BOTH); 
    exti_interrupt_flag_clear(EXTI_12);
     
    nvic_irq_disable(EXTI4_15_IRQn);
}

void EXTI12_Run(unsigned char Aon)
{
    if(Aon) {
        exti_interrupt_flag_clear(EXTI_12);
        nvic_irq_enable(EXTI4_15_IRQn, 1U);
    } else {
        nvic_irq_disable(EXTI4_15_IRQn);
    }
}

void SetPulseCounter_12(unsigned char AOn)
{
    if (AOn) {
        G_PulseCount12 = 0;
        G_PulseCount12_1 = 0;
        PulseTimeOut12 = 0;
        EXTI12_Run(1);
    } else {
        EXTI12_Run(0);
    }
}
#endif

void Timer13_Run(unsigned char Aon)
{
    if(Aon){
        timer_interrupt_flag_clear(TIMER13, TIMER_INT_FLAG_UP);
        timer_enable(TIMER13);
    }else{
        timer_disable(TIMER13);
    }
}
#ifdef SEGLCD_V2206    
    void EXTI1_Run(unsigned char Aon)
    {
        if(Aon){
            exti_interrupt_flag_clear(EXTI_1);
            nvic_irq_enable(EXTI0_1_IRQn, 1U);
        }else{
            nvic_irq_disable(EXTI0_1_IRQn);
        }
    }
#else
    void EXTI11_Run(unsigned char Aon)
    {
        if(Aon){
            exti_interrupt_flag_clear(EXTI_11);
            nvic_irq_enable(EXTI4_15_IRQn, 1U);
        }else{
            nvic_irq_disable(EXTI4_15_IRQn);
        }
    }
#endif


void Timer14_Run(unsigned char Aon)
{
    if(Aon){
         
        timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
        timer_enable(TIMER14);
    }else{
        timer_disable(TIMER14);
    }
}

void SetTime13_Counter(unsigned char AOn)
{
    if (AOn){
        Time13_Count = 0;
        Time13_Count2 =0;
        Timer13_Run (1);
    } else{
        Timer13_Run (0);
    }
}

void SetTime14_Counter(unsigned char AOn)  
{
    if (AOn){
        Time14_Count = 0;
        Time14_Count2 = 0;
        Timer14_Run ( 1 );
    }else{
        Timer14_Run ( 0 );
    }
}

void SetPulseCounter(unsigned char AOn)
{
    if (AOn) {
        G_PulseCount = 0;
        G_PulseCount2 = 0;
        PulseCounted = 0;
        PulseTimeOut = 0;
    #ifdef SEGLCD_V2206
        EXTI1_Run(1);
    #else
        EXTI11_Run(1);
    #endif
    }else {
    #ifdef SEGLCD_V2206
        EXTI1_Run(0);
    #else
        EXTI11_Run(0);
    #endif
    }
}

unsigned int GetTime13_Count()
{
    return Time13_Count;
}

void CheckStandby()
{
    if(RESET == gpio_input_bit_get(WAKEUP_KEY_GPIO_PORT, WAKEUP_KEY_PIN)){
        Valve_OFF;
        G_Standby = 1;
        SaveAmountFun();                         
        NVIC_SystemReset();
    }
}
 
void DoSleepWakeUpTask()
{
    ReadStandbyFlag();
    if(G_Standby == 1){
        G_Standby = 0;
        SaveAmountFun();
        if(RESET == gpio_input_bit_get(WAKEUP_KEY_GPIO_PORT, WAKEUP_KEY_PIN)){
            rcu_periph_clock_disable(RCU_GPIOA);
            rcu_periph_clock_disable(RCU_GPIOB);
            rcu_periph_clock_disable(RCU_GPIOC);
            rcu_periph_clock_disable(RCU_GPIOF);

            pmu_wakeup_pin_enable(PMU_WAKEUP_PIN0);
            pmu_to_standbymode();
        }
    }
}

#ifdef SYS_FWDGT
void Fwdgt_Init(void)
{
    uint16_t timeout_t = 0xFFFFU;
     
    rcu_osci_on(RCU_IRC40K);
     
     
	while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K))
	{
		if(timeout_t > 0)
			timeout_t--;
		else
			break;
	}

    dbg_periph_enable(DBG_FWDGT_HOLD);       
    fwdgt_config(1560, FWDGT_PSC_DIV128);    
    fwdgt_enable();
}

#endif
